<html>
<head><meta charset="utf-8"><title>Beginner tries to fix derive macros · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html">Beginner tries to fix derive macros</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234848871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234848871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234848871">(Apr 16 2021 at 13:03)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Clone)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Example</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="o">&gt;</span><span class="p">([();</span><span class="w"> </span><span class="n">N</span><span class="p">]);</span><span class="w"></span>
</code></pre></div>
<p>I have found the source of this bug (the error message wasn't very helpful)</p>
<div class="codehilite"><pre><span></span><code>error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
 --&gt; src/lib.rs:4:29
  |
4 | struct Example&lt;const N: usize = 1usize&gt;([(); N]);
  |                             ^

error: aborting due to previous error; 1 warning emitted
</code></pre></div>
<p>Basically compiler copy-pastes <code>const N: usize = 1</code> into the impl block, so compilation must error</p>
<p>I dug into rustc source code trying to find the derive macro code, but because I have never done it before I want to ask someone if this is the correct place (before I spend rest of the day compiling and testing this change)</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code><span class="gi">+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs</span>
<span class="gu">@@ -561,7 +561,13 @@ fn create_derived_impl(</span>

                 cx.typaram(self.span, param.ident, vec![], bounds, None)
             }
<span class="gd">-            GenericParamKind::Const { .. } =&gt; param.clone(),</span>
<span class="gi">+            GenericParamKind::Const { ty, kw_span, _default } =&gt; GenericParamKind::Const {</span>
<span class="gi">+                ty: ty.clone(),</span>
<span class="gi">+                kw_span: kw_span.clone(),</span>
<span class="gi">+</span>
<span class="gi">+                /* We can't have default values inside impl block */</span>
<span class="gi">+                default: None,</span>
<span class="gi">+            },</span>
         }));
 ```
</code></pre></div>



<a name="234849072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234849072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234849072">(Apr 16 2021 at 13:05)</a>:</h4>
<p>fyi you'll need to compile rustc either way, so I would run that in the background while you wait</p>



<a name="234850772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234850772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234850772">(Apr 16 2021 at 13:16)</a>:</h4>
<p>I'm pretty sure that the type of that match expression is <code>GenericParam</code>, but you're trying to return a <code>GenericParamKind</code>, so this won't compile.</p>



<a name="234850859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234850859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234850859">(Apr 16 2021 at 13:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Noob.20tries.20to.20fix.20derive.20macros/near/234849072">said</a>:</p>
<blockquote>
<p>fyi you'll need to compile rustc either way, so I would run that in the background while you wait</p>
</blockquote>
<p>you can run <code>x.py check</code> if you just want to make sure it compiles</p>



<a name="234850930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234850930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234850930">(Apr 16 2021 at 13:17)</a>:</h4>
<p>I run <code>x.py check; x.py build</code> a lot until <a href="https://github.com/rust-lang/cargo/issues/3501">https://github.com/rust-lang/cargo/issues/3501</a> is fixed</p>



<a name="234853104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234853104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234853104">(Apr 16 2021 at 13:31)</a>:</h4>
<blockquote>
<p>I'm pretty sure that the type of that match expression is <code>GenericParam</code>, but you're trying to return a <code>GenericParamKind</code>, so this won't compile.</p>
</blockquote>
<p>yep</p>
<div class="codehilite"><pre><span></span><code>expected struct `rustc_ast::GenericParam`, found enum `GenericParamKind`
</code></pre></div>



<a name="234859812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234859812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234859812">(Apr 16 2021 at 14:12)</a>:</h4>
<p>hey, it works! :D</p>



<a name="234859927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234859927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234859927">(Apr 16 2021 at 14:12)</a>:</h4>
<p>Where should i put a test case?</p>



<a name="234860091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234860091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234860091">(Apr 16 2021 at 14:13)</a>:</h4>
<p>somewhere in <code>src/test/ui/derives</code> seems reasonable</p>



<a name="234861489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234861489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234861489">(Apr 16 2021 at 14:22)</a>:</h4>
<p><code>error: ui test compiled successfully!</code><br>
hmmmm</p>



<a name="234861604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234861604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234861604">(Apr 16 2021 at 14:23)</a>:</h4>
<p>add <code>// check-pass</code></p>



<a name="234861607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234861607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234861607">(Apr 16 2021 at 14:23)</a>:</h4>
<p>also what does this mean?<br>
<code>skip untracked path src/test/ui/derives/derive-macro-const-default.rs during rustfmt invocations</code></p>



<a name="234861668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234861668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234861668">(Apr 16 2021 at 14:23)</a>:</h4>
<p>it's not formatting it because it's not added to git</p>



<a name="234861678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234861678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234861678">(Apr 16 2021 at 14:23)</a>:</h4>
<p>try <code>git add  src/test/ui/derives/derive-macro-const-default.rs</code></p>



<a name="234861952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234861952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234861952">(Apr 16 2021 at 14:25)</a>:</h4>
<div class="codehilite"><pre><span></span><code>failures:
    [debuginfo-gdb] debuginfo/extern-c-fn.rs
    [debuginfo-gdb] debuginfo/generator-objects.rs
    [debuginfo-gdb] debuginfo/issue-57822.rs
    [debuginfo-gdb] debuginfo/pretty-huge-vec.rs
</code></pre></div>



<a name="234862046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234862046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234862046">(Apr 16 2021 at 14:25)</a>:</h4>
<p>I would ignore those, a full x.py test is almost never what you want</p>



<a name="234862048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234862048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234862048">(Apr 16 2021 at 14:25)</a>:</h4>
<p>i haven't run <code>./x.py test</code> before changes, so idk if this is my fault :/</p>



<a name="234862062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234862062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234862062">(Apr 16 2021 at 14:25)</a>:</h4>
<p>try <code>x.py test src/test/ui/derives</code></p>



<a name="234862235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234862235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234862235">(Apr 16 2021 at 14:26)</a>:</h4>
<p>yep, everything is alright</p>



<a name="234863675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234863675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234863675">(Apr 16 2021 at 14:34)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/84243">https://github.com/rust-lang/rust/pull/84243</a></p>



<a name="234882795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Beginner%20tries%20to%20fix%20derive%20macros/near/234882795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Beginner.20tries.20to.20fix.20derive.20macros.html#234882795">(Apr 16 2021 at 16:22)</a>:</h4>
<p>Huh, this went smoother than I expected, CI is already all green</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>